package com.zhihuichongcheng.application.ui.task;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.task.adapter.UserAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * AtUserListActivity Created by LK on 2017/1/6.
 */
public class AtUserListActivity extends BaseActivity
        implements View.OnClickListener, UserAdapter.OnUserClickListener {
    public static final String EXTRA_TASK_ID = "extra_task_id";
    public static final String EXTRA_RESULT_USERS = "extra_result_users";
    protected Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            }
        }
    };
    protected HttpManager manager;
    private XRecyclerView mRecyclerView;
    private UserAdapter mAdapter;
    private int mCurrentTaskId;
    private List<UserBean> selectedUsers = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCurrentTaskId = getIntent().getIntExtra(EXTRA_TASK_ID, -1);

        setContentView(R.layout.activity_at_user_list);
        manager = HttpManager.getInstance(this);
        initView();
        initListView();
    }

    private void initView() {
        setTitle("相关人员");
        getTvArrowTitle().setVisibility(View.INVISIBLE);

        mRecyclerView = (XRecyclerView) this.findViewById(R.id.rv_user);
        mRecyclerView.setHasFixedSize(true);
    }

    private void initListView() {
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);

        mAdapter = new UserAdapter(this);
        mAdapter.setOnUserClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadData();
    }

    private void loadData() {
        DialogUtil.showLoadDiadlog(mContext);
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("userMissionId", mCurrentTaskId + "");
        //筛选条件
        manager.post(NetworkAPI.MISSION_COMMENT_AT, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(),msg);
                    return;
                }
                List<UserBean> userList = GsonUtil.paraeFromStringToList(value, UserBean.class);
                mAdapter.reSetData(userList);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "查询失败 " + value);
                DialogUtil.dismiss(getContext());
            }
        }, paramsDemo);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }

    @Override
    public void onUserClick(UserBean userBean) {
        selectedUsers.clear();
        selectedUsers.add(userBean);
        toResult();
    }

    private void toResult() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(EXTRA_RESULT_USERS, (ArrayList<? extends Parcelable>) selectedUsers);
        setResult(EXTRA_RESPONSE_CODE_SUCCESS, intent);
        finish();
    }
}
